package com.jy.adscm.operation.pebase.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeDocument;
import com.jy.adscm.operation.pebase.bean.PeDocumentLabel;
import com.jy.adscm.operation.pebase.bean.PeLabel;
import com.jy.adscm.operation.pebase.bean.QueryPeDocmentLabel;
import com.jy.adscm.operation.pebase.service.PeDocumentLabelService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-04-18
 */
@RestController
@RequestMapping("/adscm/pebase/peDocumentLabel")
public class PeDocumentLabelController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    PeDocumentLabelService peDocumentLabelService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody List<PeDocumentLabel>  peDocumentLabels, HttpServletRequest request) throws AdscmSystemException {
        boolean insert=false;
        String tokenId = request.getParameter("tokenId");
        for (PeDocumentLabel peDocumentLabel : peDocumentLabels) {
            insert=  peDocumentLabelService.insert(peDocumentLabel);
        }

        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id , HttpServletRequest request)throws AdscmSystemException {
        boolean result =false;
        String tokenId = request.getParameter("tokenId");
        boolean b = peDocumentLabelService.deleteById(id);
        return new  SimpleCoreResponse("200","删除成功",b);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody PeDocumentLabel peDocumentLabel, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = peDocumentLabelService.updateById(peDocumentLabel);
        return  new SimpleCoreResponse("200","修改成功",result);
    }

    /**
     * 通过标签数组查询文档
     * @param request
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectDocumentByLabels")
    public SimpleCoreResponse selectDocumentByLabels(@RequestBody QueryPeDocmentLabel queryPeDocmentLabel, HttpServletRequest request) throws AdscmSystemException{
        Page<PeDocument>  page=new Page<>(queryPeDocmentLabel.getCurrentPage(),queryPeDocmentLabel.getPageSize());
        page= peDocumentLabelService.selectDocumentByLabels(page,queryPeDocmentLabel);
        return  new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectLabelsByDid/{did}")
    public SimpleCoreResponse selectLabelsByDid(@PathVariable String did, HttpServletRequest request) throws AdscmSystemException{

         List<PeLabel> peLabels=  peDocumentLabelService.selectLabelsByDid(did);
        return  new SimpleCoreResponse("200","查询成功",peLabels);
    }

    @RequestMapping("select")
    public SimpleCoreResponse select( HttpServletRequest request) throws AdscmSystemException{

        List<PeDocumentLabel> peDocumentLabels = peDocumentLabelService.selectList(null);
        return  new SimpleCoreResponse("200","查询成功",peDocumentLabels);
    }

}

